package com.jj.core.rpc;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class RPCUtils {
    private static Logger logger = LoggerFactory.getLogger(RPCUtils.class);
    private Map<Class<?>, ObjConvertHandler> convertHandlers = new HashMap<>();

    @PostConstruct
    public void init(){
        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Byte.class;
            }

            @Override
            public Object convert(String value) {
                return Byte.parseByte(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return byte.class;
            }

            @Override
            public Object convert(String value) {
                return Byte.parseByte(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Short.class;
            }

            @Override
            public Object convert(String value) {
                return Short.parseShort(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return short.class;
            }

            @Override
            public Object convert(String value) {
                return Short.parseShort(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Integer.class;
            }

            @Override
            public Object convert(String value) {
                return Integer.parseInt(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return int.class;
            }

            @Override
            public Object convert(String value) {
                return Integer.parseInt(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Long.class;
            }

            @Override
            public Object convert(String value) {
                return Long.parseLong(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Double.class;
            }

            @Override
            public Object convert(String value) {
                return Double.parseDouble(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return double.class;
            }

            @Override
            public Object convert(String value) {
                return Double.parseDouble(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Float.class;
            }

            @Override
            public Object convert(String value) {
                return Float.parseFloat(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return float.class;
            }

            @Override
            public Object convert(String value) {
                return Float.parseFloat(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return String.class;
            }

            @Override
            public Object convert(String value) {
                return value;
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Boolean.class;
            }

            @Override
            public Boolean convert(String value) {
                return Boolean.parseBoolean(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return boolean.class;
            }

            @Override
            public Boolean convert(String value) {
                return Boolean.parseBoolean(value);
            }
        });

        regHandler(new ObjConvertHandler() {
            @Override
            public Class<?> getParameterCls() {
                return Map.class;
            }

            @Override
            public Object convert(String value) {
                Map map = JSON.parseObject(value, Map.class);
                return map;
            }
        });
    }

    public void regHandler(ObjConvertHandler convertHdl){
        synchronized (convertHandlers){
            convertHandlers.put(convertHdl.getParameterCls(), convertHdl);
        }
    }

    public Object convert(Class<?> cls, String value){
        ObjConvertHandler hdl = convertHandlers.get(cls);
        if(hdl != null){
            return hdl.convert(value);
        }else{
            logger.error("convert handler not found for {}", cls.getSimpleName());
            return null;
        }
    }

    public interface ObjConvertHandler{
        Class<?> getParameterCls();
        Object convert(String value);
    }


}
